new Promise((resolve, reject) => {
    resolve('aaa')
})

function Promise(executor) {
    let _this = this;
    _this.state = 'pending',
        _this.data = undefined,
        _this.onResolvedCallback = [],
        _this.onRejectedCallback = [];

    function resolve(value) {
        if (_this.state === 'pending') {
            _this.state = 'resolved'
            _this.data = value;
            for (let i = 0; i < _this.onResolvedCallback.length; i++) {
                _this.onResolvedCallback[i](value)
            }
        }
    }

    function reject(reason) {
        if (_this.state === 'pending') {
            _this.state = 'rejected'
            _this.data = reason;
            for (let i = 0; i < _this.onRejectedCallback.length; i++) {
                _this.onRejectedCallback[i](reason)
            }
        }
    }
    // executor 可能会出错,那么捕获他,交给reject

    try {
        executor(resolve, reject)
    } catch (error) {
        reject(error)
    }

}

Promise.prototype.then = function (onResolve, onReject) {
    let _this = this;
    let newPromise;
    onResolve = typeof onResolve == 'function' ? onResolve : function () { };
    onReject = typeof onReject == 'function' ? onReject : function () { };

    if (_this.state === 'resolved') {
        return newPromise = new Promise((resolve, reject) => {
            try {
                x = onReject(_this.data)
                if(x instanceof Promise){
                    x.then(resolve, reject)
                }
                // 不是2
                resolve(x)
            } catch (error) {
                onReject(error)
            }
        })
    }
    if (_this.state === 'rejected') {

    }

    if (_this.state === 'pending') {

    }

}